package com.wlzg.collectiontool.view

import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.View.OnClickListener
import androidx.activity.viewModels
import com.blankj.utilcode.util.SPUtils
import com.bumptech.glide.Glide
import com.wlzg.collectiontool.R
import com.wlzg.collectiontool.model.bean.GeneralDto
import com.wlzg.collectiontool.model.bean.LoginBean
import com.wlzg.collectiontool.util.B64BitmapUtil
import com.wlzg.collectiontool.viewmodel.LoginModel
import kotlinx.android.synthetic.main.login_activity.*


class LoginActivity : BaseActivity() {
    private val TAG="LoginActivity"
    val vmodel: LoginModel by viewModels()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.login_activity)
        if(SPUtils.getInstance().getBoolean("Mlogin")){
            openActivity(MCollectionActivity::class.java)
        }
        login.setOnClickListener(listenner)
        yanzheng.setOnClickListener(listenner)
        vmodel.loginVer()
        mSubscribe()
    }

    private fun mSubscribe() {
      vmodel.u?.observe(this){
          Log.d(TAG,it.result!!)
          val bitMap=B64BitmapUtil.base64ToBitmap(it.result)
          val drawable: Drawable = BitmapDrawable(bitMap)
          Glide.with(this).load(drawable).into(yanzheng)
      }
        vmodel.succ?.observe(this){
            Log.d("hsjjsj","it")
            when(it){
                0-> openActivity(MCollectionActivity::class.java)
            }
        }
    }

    var listenner = object : OnClickListener {
        override fun onClick(p0: View?) {
            when (p0?.id) {
                R.id.login -> {
                   val loginBean=LoginBean(vert.text.toString(),"1630032716",pwd.text.toString(),userN.text.toString())
                    vmodel.mlogin(loginBean)

                }
                R.id.yanzheng -> {

                    vmodel.loginVer()

                }
            }
        }

    }
}